package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiU_discover;
import cn.com.api.entity.ApiU_discover_resources;
import cn.com.api.entity.ApiU_discover_spec;
import cn.com.api.entity.ApiU_message;
import cn.com.api.mapper.ApiU_discoverMapper;
import cn.com.api.mapper.ApiU_discover_resourcesMapper;
import cn.com.api.mapper.ApiU_discover_specMapper;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.service.ApiU_discoverService;
import cn.com.api.service.impl.base.U_discoverServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiU_discoverServiceImpl extends U_discoverServiceImplBase implements ApiU_discoverService {

	@Autowired
	private ApiU_discoverMapper apiU_discoverMapper;
	
	@Autowired
	private ApiU_discover_specMapper apiU_discover_specMapper;
	
	@Autowired
	private ApiU_discover_resourcesMapper apiU_discover_resourcesMapper;
	
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;

	// 新增对象后返回记录ID
	public int U_discoverInsertion(ApiU_discover u_discover) {
		this.apiU_discoverMapper.U_discoverInsertion(u_discover);
		return u_discover.getId();
	}

	@Transactional
	public int U_discoverInsertionWithSpec(ApiU_discover u_discover, List<ApiU_discover_spec> u_discover_specs, List<ApiU_discover_resources> u_discover_resources) {
		int discoverId = this.U_discoverInsertion(u_discover);

		Iterator<ApiU_discover_spec> iterator = u_discover_specs.iterator();
		
		for ( ; iterator.hasNext(); ) {
			ApiU_discover_spec u_discover_spec = iterator.next();
			
			u_discover_spec.setConn_id(new Integer(discoverId).toString());
			
			this.apiU_discover_specMapper.insertU_discover_spec(u_discover_spec);
		}
		
		/**huangshi001 add start*/
		Iterator<ApiU_discover_resources> iterator_resources = u_discover_resources.iterator();
		
		for ( ; iterator_resources.hasNext(); ) {
			ApiU_discover_resources u_discover_res = iterator_resources.next();
			
			u_discover_res.setConn_id(new Integer(discoverId).toString());
			
			this.apiU_discover_resourcesMapper.insertU_discover_resources(u_discover_res);
		}
		/**huangshi001 add end*/
		return discoverId;
	}
	
	@Transactional
	public int U_discoverupdateWithSpec(ApiU_discover u_discover, List<ApiU_discover_spec> u_discover_specs, List<ApiU_discover_resources> u_discover_resources) {
		
		int rs = this.apiU_discoverMapper.update_discover_info(u_discover);		
		int discoverid = u_discover.getId();		
		Iterator<ApiU_discover_spec> iterator = u_discover_specs.iterator();	
		this.apiU_discover_specMapper.deleteU_discover_spec_byconnid(discoverid);
		
		for ( ; iterator.hasNext(); ) {
			ApiU_discover_spec u_discover_spec = iterator.next();			
			u_discover_spec.setConn_id(new Integer(discoverid).toString());		
			this.apiU_discover_specMapper.insertU_discover_spec(u_discover_spec);
		}
		
		Iterator<ApiU_discover_resources> iterator_resources = u_discover_resources.iterator();		
		this.apiU_discover_resourcesMapper.deleteU_discover_resources_byconnid(discoverid);		
		for ( ; iterator_resources.hasNext(); ) {
			ApiU_discover_resources u_discover_res = iterator_resources.next();			
			u_discover_res.setConn_id(new Integer(discoverid).toString());			
			this.apiU_discover_resourcesMapper.insertU_discover_resources(u_discover_res);
		}	
		return rs;
	}

	
	
	@Transactional
	public int U_discoverUpdateWithSpec(ApiU_discover u_discover, List<ApiU_discover_spec> u_discover_specs) {
		int rs = this.apiU_discoverMapper.update_discover(u_discover);
		
		List<ApiU_discover_spec> originSpecs = this.apiU_discover_specMapper.getApiU_discover_specsBydiscover_Id(u_discover.getId().toString());
		
		Iterator<ApiU_discover_spec> iterator = originSpecs.iterator();
		for ( ; iterator.hasNext(); ) {
			this.apiU_discover_specMapper.deleteU_discover_spec(iterator.next().getId());
		}
		
		iterator = u_discover_specs.iterator();
		
		for ( ; iterator.hasNext(); ) {
			ApiU_discover_spec u_discover_spec = iterator.next();
			
			u_discover_spec.setConn_id(u_discover.getId().toString());
			
			this.apiU_discover_specMapper.insertU_discover_spec(u_discover_spec);
		}
		
		return rs;
	}

	public int U_discoverVerify(ApiU_discover u_discover) {
		return this.apiU_discoverMapper.U_discoverVerify(u_discover);
	}

	@Override
	public List<ApiU_discover> getU_discoverByUser(String userId) {
		return this.apiU_discoverMapper.getU_discoverByUser(userId);
	}
	
	public List<ApiU_discover> getU_discoverByUserAbility(String[] userlabels, Integer type) {
		List<ApiU_discover> results = new ArrayList<ApiU_discover>();
		for(int i=0; i<userlabels.length; i++) {
			List<ApiU_discover> discovers_of_label = this.apiU_discoverMapper.getU_discoverByUserLabels(userlabels[i], type);
			results.addAll(discovers_of_label);
		}
		return results;
	}

	@Transactional
	public int U_discoverInsertionWithSpec(ApiU_discover u_discover,
			List<ApiU_discover_spec> u_discover_specs) {
		int discoverId = this.U_discoverInsertion(u_discover);

		Iterator<ApiU_discover_spec> iterator = u_discover_specs.iterator();
		
		for ( ; iterator.hasNext(); ) {
			ApiU_discover_spec u_discover_spec = iterator.next();
			
			u_discover_spec.setConn_id(new Integer(discoverId).toString());
			
			this.apiU_discover_specMapper.insertU_discover_spec(u_discover_spec);
		}
		
		return discoverId;
	}
	
	public List<Map<String, Object>> searchDiscover(Map<String, Object> conditions, PageInfo pageInfo) {
		return this.apiU_discoverMapper.searchDiscoverPage(conditions, pageInfo);
	}

	
	@Override
	public List<ApiU_discover> findU_discovers_exhibitionByMap(
			Map<String, Object> params, PageInfo pageInfo) {
		
		return this.apiU_discoverMapper.find_discover_exhibition(params, pageInfo);
	}

	@Override
	public List<ApiU_discover> getU_discoverByJoinUser(Map<String, Object> paramMap) {
		
		return this.apiU_discoverMapper.getU_discoverByJoinUser(paramMap);
	}

	@Override
	public void insertU_discoverShare(Map<String, String> paramMap) {
		
		 this.apiU_discoverMapper.insertU_discoverShare(paramMap);
		
	}

	@Override
	public ApiU_discover findU_discovers_detail(Map<String, String> params) {
		
		 ApiU_discover apiU_discover  = this.apiU_discoverMapper.findU_discovers_detail(params);
		 
		 Map<String, Object> map = this.apiU_discoverMapper.getdescription(params);
		 if(map.get("DESCRIPTION") != null){
			 apiU_discover.setDescription((String)map.get("DESCRIPTION"));
		 }
		return apiU_discover;
	}

	@Override
	public List<ApiU_discover> find_discover_list(Map<String, Object> params, PageInfo pageInfo) {
		
		return this.apiU_discoverMapper.find_discover_list(params, pageInfo);
	}

	@Override
	public int find_discover_join_count(Map<String, Object> params) {
		
		return this.apiU_discoverMapper.find_discover_join_count(params);
	}

	@Override
	public int find_discover_publish_count(Map<String, Object> params) {
		
		return this.apiU_discoverMapper.find_discover_publish_count(params);
	}

	@Override
	public List<ApiU_discover> findFavoriteDiscoverList(String cate_type, String user_id,
			Integer type, PageInfo page) {
		
		return this.apiU_discoverMapper.findFavoriteDiscoverListPage(cate_type, user_id, type, page);

	}

	@Override
	public List<ApiU_discover> search_discover_info(
			Map<String, Object> conditions, PageInfo pageInfo) {
		
		return this.apiU_discoverMapper.search_discover_info(conditions, pageInfo);
	}

	@Override
	public List<Map<String, Object>> find_discover_by_loc(
			Map<String, Object> params) {
		String cate_type = (String)params.get("cate_type");
		if(cate_type != null && cate_type.equals("5")){
			return apiU_discoverMapper.find_discover_by_loc_service(params);
		}
		
		return apiU_discoverMapper.find_discover_by_loc_need(params);
	}

	@Override
	public List<Map<String, String>> selectByOrListPage(
			Map<String, Object> cond, PageInfo page) {
		
		return apiU_discoverMapper.selectByOrListPage(cond, page);
	}

	@Override
	public int findU_discovers_status_byUserid(Map<String, String> params) {
		
		return apiU_discoverMapper.findU_discovers_status_byUserid(params);		
	}

	@Override
	public int U_discover_delete(Integer id) {
		
		return apiU_discoverMapper.U_discover_delete(id);
	}
     

	@Override
	public int U_discovercancel(Integer id) {
		
		return apiU_discoverMapper.U_discovercancel(id);
	}

	@Transactional
	@Override
	public void findTaskInfo(Map<String, Object> param) {
		
		int type = (int)param.get("type");
		Map<String, Object> result = apiU_discoverMapper.findTaskInfo(param);
		String cate_name = (String)result.get("CATE_NAME");
		
		if(type == 4){
			param.put("type", 5); //获得可以接单的类型,需求变成服务，服务变成需求
		}else{					
			param.put("type", 4);			
		}
		param.put("cate_name", cate_name);
		List<Map<String, Object>> user_info = apiU_discoverMapper.findToTaskInfo(param);
		for (Map<String, Object> map : user_info) {
			
			//TODO 插入一条message消息
			ApiU_message message = new ApiU_message();
			message.setConn_id((int)param.get("id"));
			message.setType(11);//默认写死11代表  新发现订单提醒
			message.setAdd_time(Util.CurrentTime());
			message.setIs_read(0);
			message.setUser_id(map.get("USER_ID")+"");
			message.setTitle("附近有新的订单啦!");
			message.setContent(result.get("TITLE")+"");
			this.apiU_messageMapper.U_messageInsertion(message);
		}
	}
}
